package com.zyz.I_多线程.单例模式.懒汉式;

/**
 * @author: vick
 * @Date: 2025/3/22
 * @Description:
 * @version: 1.0
 */
public class Singleton {
    private static Singleton instance;

    //为了不让new，产生不一样的对象，所以构造器私有化
    private Singleton() {

    }

    //方式一
    /*public static synchronized Singleton getInstance() {
        if (instance == null) { //第一次调用。就实例化
            instance = new Singleton();
        }
        return instance;
    }*/

    //方式二
    public static Singleton getInstance() {
        if (instance == null) { //第一次调用。就实例化,
            synchronized (Singleton.class) { //双重检测
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
